//
//  NYCommentView.m
//  iMobile
//
//  Created by baken on 2016/12/21.
//  Copyright © 2016年 MyApp. All rights reserved.
//

#import "NYCommentView.h"

@interface NYCommentView()<UITextFieldDelegate>

//@property (nonatomic, strong) id delegate;

@end

@implementation NYCommentView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = UIColorFromRGB(0xededed);
        [self initSubViews];
    }
    return self;
}

- (void)initSubViews{
    self.rightButton = [[UIButton alloc] init];
    [self.rightButton addTarget:self action:@selector(shareBarButtonItemTapped) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.rightButton];
    
    [self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-15);
        make.centerY.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(35, 35));
    }];
    
    
    _textfield = [[UITextField alloc] init];
    _textfield.delegate = self;
    _textfield.layer.masksToBounds = YES;
    _textfield.layer.cornerRadius = 17;
    _textfield.backgroundColor = UIColorFromRGB(kAllWhiteColor);
    _textfield.clearButtonMode = UITextFieldViewModeWhileEditing;
    _textfield.returnKeyType = UIReturnKeySearch;
    
    [self addSubview:_textfield];
    [_textfield mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.centerY.equalTo(self);
        make.right.equalTo(self.rightButton.mas_left).offset(-15);
        make.height.mas_equalTo(35);
    }];
    
    
    UIImageView *searchImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"comment"]];
    _textfield.leftView = searchImage;
    _textfield.leftViewMode = UITextFieldViewModeAlways;
    
}


- (void)setHolderText:(NSString *)holderText{
    _holderText = holderText;
    _textfield.placeholder = holderText;
}

-(void)setBorderColor:(UIColor *)borderColor{
    _borderColor = borderColor;
    _textfield.layer.borderColor = borderColor.CGColor;
    
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    /*
     每点击一次键盘执行一次
     当输入的时候textField.text是当前textField中已经存在的文字；
     range中的location表示当前已经输入的字符中准备change的哪个字符的range；range.location表示即将改变的字符在已经text中的位置；range.length表示要改变的text的长度；如果是删除，要删掉一个text的字符所以是1，新输入的字符不改变原text所以是0
     rang。localtion是当前已输入文字的长度；
     每输入一次string就是当前正在输入的文字
     textField.text +string就是当前textField中全部要输入的文字
     */
    
    //    [_delegate filterHistoryResult:textField.text andRange:range andReplaceString:string];
    return YES;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if ([_delegate respondsToSelector:@selector(beginEdit)]) {
        [textField resignFirstResponder];
        [_delegate beginEdit];
    }
    
    return NO;
}

- (void)shareBarButtonItemTapped{
    if ([_delegate respondsToSelector:@selector(shareNews)]) {
        [_delegate shareNews];
    }
    
}

@end
